package com.springstudy.processor;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Proxy;
import java.time.LocalDateTime;

/**
 * @author Tamako
 * @since 2025/1/8 10:19
 */
public class TimeLogBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        //使用动态代理对目标增强，返回proxy对象，进而储存到单列池中
        return Proxy.newProxyInstance(
                bean.getClass().getClassLoader(),
                bean.getClass().getInterfaces(),
                (proxy, method, args) -> {
                    System.out.println("方法" + method.getName() + "-开始时间" + LocalDateTime.now());
                    Object result = method.invoke(bean, args);
                    System.out.println("方法" + method.getName() + "-结束时间" + LocalDateTime.now());
                    return result;
                }
        );
    }
}
